using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using SLProxy;
using libsecondlife;
using libsecondlife.Packets;
using System.Net;

namespace StickyChat
{
    public class StickyChat : UserControl, StickIn
    {
        private StickyServices loader;
        private GroupBox StickyChannel;
        private NumericUpDown StickyChannelValue;
        private CheckBox Echo;
        private GroupBox Sticky;
        private RadioButton Shout;
        private RadioButton Whisper;
        private RadioButton Off;
        private string myName;
    
        public StickyChat(StickyServices p)
        {
            loader = p;
            myName = "Sticky Chat";
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.StickyChannel = new System.Windows.Forms.GroupBox();
            this.StickyChannelValue = new System.Windows.Forms.NumericUpDown();
            this.Echo = new System.Windows.Forms.CheckBox();
            this.Sticky = new System.Windows.Forms.GroupBox();
            this.Shout = new System.Windows.Forms.RadioButton();
            this.Whisper = new System.Windows.Forms.RadioButton();
            this.Off = new System.Windows.Forms.RadioButton();
            this.StickyChannel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.StickyChannelValue)).BeginInit();
            this.Sticky.SuspendLayout();
            this.SuspendLayout();
            // 
            // StickyChannel
            // 
            this.StickyChannel.Controls.Add(this.StickyChannelValue);
            this.StickyChannel.Controls.Add(this.Echo);
            this.StickyChannel.Location = new System.Drawing.Point(0, 15);
            this.StickyChannel.Name = "StickyChannel";
            this.StickyChannel.Size = new System.Drawing.Size(147, 43);
            this.StickyChannel.TabIndex = 4;
            this.StickyChannel.TabStop = false;
            this.StickyChannel.Text = "Sticky Channel";
            // 
            // StickyChannelValue
            // 
            this.StickyChannelValue.Location = new System.Drawing.Point(7, 17);
            this.StickyChannelValue.Maximum = new decimal(new int[] {
            2147483647,
            0,
            0,
            0});
            this.StickyChannelValue.Minimum = new decimal(new int[] {
            -2147483648,
            0,
            0,
            -2147483648});
            this.StickyChannelValue.Name = "StickyChannelValue";
            this.StickyChannelValue.Size = new System.Drawing.Size(54, 20);
            this.StickyChannelValue.TabIndex = 1;
            this.StickyChannelValue.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // Echo
            // 
            this.Echo.AutoSize = true;
            this.Echo.Location = new System.Drawing.Point(66, 19);
            this.Echo.Name = "Echo";
            this.Echo.Size = new System.Drawing.Size(80, 17);
            this.Echo.TabIndex = 0;
            this.Echo.Text = "&Echo Silent";
            // 
            // Sticky
            // 
            this.Sticky.Controls.Add(this.Shout);
            this.Sticky.Controls.Add(this.Whisper);
            this.Sticky.Controls.Add(this.Off);
            this.Sticky.Location = new System.Drawing.Point(0, -20);
            this.Sticky.Name = "Sticky";
            this.Sticky.Size = new System.Drawing.Size(149, 41);
            this.Sticky.TabIndex = 3;
            this.Sticky.TabStop = false;
            this.Sticky.Text = "Sticky Chat";
            // 
            // Shout
            // 
            this.Shout.AutoSize = true;
            this.Shout.Location = new System.Drawing.Point(98, 19);
            this.Shout.Name = "Shout";
            this.Shout.Size = new System.Drawing.Size(53, 17);
            this.Shout.TabIndex = 2;
            this.Shout.Text = "&Shout";
            // 
            // Whisper
            // 
            this.Whisper.AutoSize = true;
            this.Whisper.Location = new System.Drawing.Point(37, 19);
            this.Whisper.Name = "Whisper";
            this.Whisper.Size = new System.Drawing.Size(64, 17);
            this.Whisper.TabIndex = 1;
            this.Whisper.Text = "&Whisper";
            // 
            // Off
            // 
            this.Off.AutoSize = true;
            this.Off.Checked = true;
            this.Off.Location = new System.Drawing.Point(1, 20);
            this.Off.Name = "Off";
            this.Off.Size = new System.Drawing.Size(39, 17);
            this.Off.TabIndex = 0;
            this.Off.TabStop = true;
            this.Off.Text = "&Off";
            // 
            // StickyChat
            // 
            this.Controls.Add(this.StickyChannel);
            this.Controls.Add(this.Sticky);
            this.Name = "StickyChat";
            this.Size = new System.Drawing.Size(149, 56);
            this.StickyChannel.ResumeLayout(false);
            this.StickyChannel.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.StickyChannelValue)).EndInit();
            this.Sticky.ResumeLayout(false);
            this.Sticky.PerformLayout();
            this.ResumeLayout(false);

        }

        #region StickIn Members

        public string name
        {
            get { return myName; }
        }

        public void start()
        {
            loader.proxy.AddDelegate(PacketType.ChatFromViewer, Direction.Outgoing, ChatFromViewer);
        }

        public void stop()
        {
            loader.proxy.RemoveDelegate(PacketType.ChatFromViewer, Direction.Outgoing, ChatFromViewer);
        }
        #endregion StickIn Members

        private Packet ChatFromViewer(Packet packet, IPEndPoint sim)
        {
            // deconstruct the packet
            ChatFromViewerPacket chat = (ChatFromViewerPacket)packet;
            string message = Helpers.FieldToUTF8String(chat.ChatData.Message);
            int channel = chat.ChatData.Channel;
            byte type = chat.ChatData.Type;

            if (type == 1)
            {
                if (Shout.Checked)
                {
                    chat.ChatData.Type = 2;
                }
                else if (Whisper.Checked)
                {
                    chat.ChatData.Type = 0;
                }
            }
            if (type < 3 && channel == 0 && (int)StickyChannelValue.Value != 0)
            {
                chat.ChatData.Channel = (int)StickyChannelValue.Value;
            }
            if (chat.ChatData.Channel != 0 && Echo.Checked)
            {
                if (chat.ChatData.Type == 1)
                {
                    loader.SayToUser("You/" + chat.ChatData.Channel, message);
                }
                else if (chat.ChatData.Type == 0)
                {
                    loader.SayToUser("You whisper/" + chat.ChatData.Channel, message);
                }
                else if (chat.ChatData.Type == 2)
                {
                    loader.SayToUser("You shout/" + chat.ChatData.Channel, message);
                }
            }

            return chat;
        }
    }
}